//
//  MTUmengLogin.swift
//  MT
//
//  Created by ooops on 2019/12/21.
//  Copyright © 2019 张强. All rights reserved.
//

import Foundation

enum MTLoginType {
    case qq, wechat, sinaWeibo
}


class MTUmengLogin: NSObject {

    class func registApp() {
        // 打开调试模式日志
        UMConfigure.initWithAppkey(AppConfig.umengAppKey, channel: "App Store")
        #if DEBUG
        UMSocialManager.default().openLog(true)
        #else
        UMSocialManager.default().openLog(false)
        #endif
        
        // 关闭强制验证https, 即可分享http链接图片
        UMSocialGlobal.shareInstance().isUsingHttpsWhenShareContent = false
    
       //注册微信
        UMSocialManager.default().setPlaform(UMSocialPlatformType.wechatSession, appKey: AppConfig.wechatAppKey, appSecret: AppConfig.wechatAppSecret, redirectURL: "")
        //注册QQ
        //UMSocialManager.default().setPlaform(UMSocialPlatformType.QQ, appKey: "", appSecret: nil, redirectURL: "")
        
        // 注册新浪微博
        //UMSocialManager.default().setPlaform(UMSocialPlatformType.sina, appKey: "", appSecret: "", redirectURL: kLQUmengSDK_SinaWeibOredirectUri)
    }
    
    @discardableResult
    class func handle(_ url: URL, source: String? = nil, annotation: Any? = nil, options: [String: Any]? = nil) -> Bool {
        
        if let opt = options {
            return UMSocialManager.default().handleOpen(url, options: opt)
        } else if let ant = annotation {
            return UMSocialManager.default().handleOpen(url,sourceApplication: source, annotation: ant)
        } else {
            return UMSocialManager.default().handleOpen(url)
        }
    }
    
    class func isWechatInstall() -> Bool {
        
        return UMSocialManager.default().isInstall(UMSocialPlatformType.wechatSession)
    }
    
    /// 登录
    ///
    /// - Parameters:
    ///   - type: 登录类型
    ///   - viewController: 当前控制器
    ///   - handle: 成功回调
    ///   - failed: 失败回调
    class func login(_ type: MTLoginType, onViewController viewController: UIViewController? = nil, success handle: @escaping ((_ result: UMSocialUserInfoResponse) -> Void), failed: ((_ error: Error) -> Void)?) {
        
        var loginType: UMSocialPlatformType? = nil
        
        if type == .qq {
            loginType = UMSocialPlatformType.QQ
        } else if type == .wechat {
            loginType = UMSocialPlatformType.wechatSession
        } else if type == .sinaWeibo {
            loginType = UMSocialPlatformType.sina
        }
        
        if let login = loginType {
            // 取消上次授权
            UMSocialManager.default().cancelAuth(with: login) { (result, error) in }
            
            UMSocialManager.default().getUserInfo(with: login, currentViewController: viewController) { (result, error) in
                
                if let resp = result as? UMSocialUserInfoResponse {
                    handle(resp)
                } else {
                    if let hd = failed {
                        hd(error!)
                    }
                }
            }
        }
    }
}
